package backtracking;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author: 海琳琦
 * @Date: 2022/2/26 14:45
 * https://leetcode-cn.com/problems/increasing-subsequences/
 * 递增子序列
 */
public class FindSubsequences {

    static List<List<Integer>> result;
    static LinkedList<Integer> list;


    public List<List<Integer>> findSubsequences(int[] nums) {
        result= new ArrayList<>();
        list = new LinkedList<>();
        backTracking(nums, 0);
        return result;
    }

    private void backTracking(int[] nums, int start) {
        if (list.size() >= 2) {
            result.add(new ArrayList<>(list));
        }
        //定义树本层的去重标志数组
        int[] flag = new int[201];
        for (int i = start; i < nums.length; i++) {
            //树的第一层不考虑最后一个元素
//            if (start == 0 && i == nums.length - 1) {
//                break;
//            }
            //树的第2/3/4...层考虑结点间大小和是否重复
            if (start != 0 && nums[i] < list.getLast() ) {
                continue;
            }
            //树第一层也需要考虑重复问题 即start==0时也要考虑
            if (flag[nums[i] + 100] == 1) {
                continue;
            }
            flag[nums[i] + 100] = 1;
            list.add(nums[i]);
            backTracking(nums, i + 1);
            list.removeLast();
        }
    }

    public static void main(String[] args) {

    }
}
